package com.lancoo.znbkxx.teacher.ui.fragment

import android.annotation.SuppressLint
import android.graphics.BitmapFactory
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.HomeWorkAdapter
import com.lancoo.znbkxx.teacher.adapter.SpacesItemDecoration
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper.AibkGeneratePaperActivity
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkReviewTestPaper.AibkTestPaperReviewActvity
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.databinding.FmtTutorBinding
import com.lancoo.znbkxx.teacher.model.data.GrammarInfo
import com.lancoo.znbkxx.teacher.model.data.LanguageInfo
import com.lancoo.znbkxx.teacher.model.data.PaperBaseInfo
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.viewmodel.PaperViewModel

import kotlinx.android.synthetic.main.fmt_tutor.*


/**
 * @author : Admin
 * @date   : 2020/10/23
 * @desc   :
 */
class HomeWorkFragment : BaseVMFragment<PaperViewModel, FmtTutorBinding>(),
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener,
        View.OnClickListener {
    private var pageSize = 10
    private val layer by lazy { arguments?.getInt("layer") }
    private val stuID by lazy { arguments?.getString("stuID") }
    private val grammarInfo by lazy {
        requireArguments().getParcelableArrayList<GrammarInfo>("grammar")
    }
    private val languageInfo by lazy {
        requireArguments().getParcelableArrayList<LanguageInfo>("language")
    }
    private lateinit var homeWorkAdapter: HomeWorkAdapter
    private val homeList = mutableListOf<PaperBaseInfo>()
    override fun providerContext(): Any? {
        return this
    }

    override fun providerVMClass(): Class<PaperViewModel>? {
        return PaperViewModel::class.java
    }

    override fun setLayoutID(): Int {
        return R.layout.fmt_tutor
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        homeWorkAdapter = HomeWorkAdapter(homeList)
        val emptyView = layoutInflater.inflate(R.layout.item_empty_tutor, null)
        homeWorkAdapter.onItemClickListener = this
        homeWorkAdapter.emptyView = emptyView
        if (layer == 0) {
            try {
                val bmp = BitmapFactory.decodeResource(resources, R.mipmap.aibk_icon_star)
                val height = bmp.height
                val layoutParams = rating_bar.layoutParams
                layoutParams.height = height
                layoutParams.width = ConstraintLayout.LayoutParams.WRAP_CONTENT
                rating_bar.layoutParams = layoutParams
                rating_bar.rating =
                    if (grammarInfo!!.size > 0) grammarInfo!![0].GrammarRecommendation.toFloat() else 5f
            } catch (e: Exception) {
                e.printStackTrace()
            }
            binding.ivIcon.setImageResource(R.mipmap.icon_noresult)
            binding.tvConclusion.text = "语法知识暂无诊断结果"
            binding.tvInfo.text = ""
            grammarInfo?.let {
                if (it.size > 0) {
                    when (it[0].Conclusion) {
                        0->{
                            binding.ivIcon.setImageResource(R.mipmap.icon_noresult)
                            binding.tvConclusion.text = "语法知识暂无诊断结果"
                            binding.tvInfo.text = ""
                        }
                        1 -> {
                            binding.ivIcon.setImageResource(R.mipmap.icon_welldone1)
                            binding.tvConclusion.text = "语法知识薄弱"
                            binding.tvInfo.text = "${Ext.TESTNAME}常考${
                            if (it[0].GraspStateList.size > 1) {
                                it[0].GraspStateList[0] + "、" + it[0].GraspStateList[1]
                            } else {
                                it[0].GraspStateList[0]
                            }
                            }等${it[0].GraspStateList.size}个专题掌握较差~要努力噢！"
                        }
                        2 -> {
                            binding.ivIcon.setImageResource(R.mipmap.icon_welldone2)
                            binding.tvConclusion.text = "语法知识一般"
                            binding.tvInfo.text = "${Ext.TESTNAME}常考${
                            if (it[0].GraspStateList.size > 1) {
                                it[0].GraspStateList[0] + "、" + it[0].GraspStateList[1]
                            } else {
                                it[0].GraspStateList[0]
                            }
                            }等${it[0].GraspStateList.size}个专题掌握需巩固~要加油噢！"
                        }
                        else -> {
                            binding.ivIcon.setImageResource(R.mipmap.icon_welldone3)
                            binding.tvConclusion.text = "语法知识较好"
                            binding.tvInfo.text = "${Ext.TESTNAME}常考${
                            if (it[0].GraspStateList.size > 1) {
                                it[0].GraspStateList[0] + "、" + it[0].GraspStateList[1]
                            } else  {
                                it[0].GraspStateList[0]
                            } 
                            }等${it[0].GraspStateList.size}个专题掌握较好~继续保持！"
                        }
                    }
                }
            }
        } else {
            try {
                val bmp = BitmapFactory.decodeResource(resources, R.mipmap.aibk_icon_star)
                val height = bmp.height
                val layoutParams = rating_bar.layoutParams
                layoutParams.height = height
                layoutParams.width = ConstraintLayout.LayoutParams.WRAP_CONTENT
                rating_bar.layoutParams = layoutParams
                rating_bar.rating =
                    if (languageInfo!!.size > 0) languageInfo!![0].LanguageRecommendation.toFloat() else 5f
            } catch (e: Exception) {
                e.printStackTrace()
            }
            binding.ivIcon.setImageResource(R.mipmap.icon_noresult)
            binding.tvConclusion.text = "综合能力暂无诊断结果"
            binding.tvInfo.text = ""
            languageInfo?.let {
                if (it.size > 0) {
                    when (it[0].ZH) {
                        "1" -> binding.ivIcon.setImageResource(R.mipmap.icon_welldone1)
                        "2" -> binding.ivIcon.setImageResource(R.mipmap.icon_welldone2)
                        "3" -> binding.ivIcon.setImageResource(R.mipmap.icon_welldone3)
                    }
                    if (it[0].ZHText.isNotEmpty()) {
                        binding.tvConclusion.text = it[0].ZHText
                        binding.tvInfo.text = it[0].OtherText
                    } else if (it[0].OtherText.isNotEmpty()) {
                        binding.tvConclusion.text = it[0].OtherText
                        binding.tvInfo.text = it[0].NoneText
                    }
                }
            }
        }
        binding.tvNewbuilt.setOnClickListener(this)
        homeWorkAdapter.setHeaderAndEmpty(true)
        val map = hashMapOf<String, Int>()
        map[SpacesItemDecoration.TOP_SPACE] = DensityUtils.dp2px(requireContext(), 10f)
        val itemDecoration = SpacesItemDecoration(map)
        binding.rv.addItemDecoration(itemDecoration)
        val manager = LinearLayoutManager(activity)
        binding.rv.layoutManager = manager
        binding.rv.adapter = homeWorkAdapter
        viewModel.getPaperBaseInfoList(CurrentUser.getToken(), CurrentUser.getUserID(), stuID!!,
                CurrentUser.getSchoolID(), layer!!, 1, 999, 0)
    }

    override fun startObserve() {
        viewModel.mPaperBaseInfoList.observe(this, Observer {
            homeList.clear()
            homeList.addAll(it.List)
            homeWorkAdapter.setNewData(homeList)
        })
    }


    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

    }


    override fun onClick(v: View?) {
        //0：语法专项  1:语言能力
        AibkGeneratePaperActivity.launch(requireContext(), layer!!, stuID!!)
    }


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        //        Log.e("onItemChildClick","==点击了这里==试卷审核==")
        val paperId = homeList[position].PaperID
        AibkTestPaperReviewActvity.launch(requireContext(), layer!!,paperId, stuID!!)
    }

}